import dayjs from "dayjs";

const barOpacity = 0.6;
const lineOpacity = 0.6;

const arrangeData = (data, tags, begTime, dataStep) => {
  let content = new Array();
  let time = dayjs(begTime);
  for (let i = 0; i < data.length; i += dataStep) {
    let moment = time.format("YYYY-MM-DD HH:mm:ss");
    let timestamp = time.valueOf();
    let timestring = time.format("HH:mm");
    let scaleX = timestring + " +" + dataStep * 15;
    let cell = {
      moment,
      timestamp,
      timestring,
      scaleX,
    };
    for (let j = 0; j < tags.length; j++) {
      cell[tags[j] + "_D"] = data[i][j][0];
      cell[tags[j] + "_H"] = data[i][j][1];
    }
    content.push(cell);
    time = time.add(dataStep * 15, "minute");
  }
  return content;
};

export default function ({
  data,
  tags,
  begTime,
  dataStep,
  dataType,
  dataTime,
}) {
  let content = arrangeData(data, tags, begTime, dataStep);
  console.log("DataResolver", { data, tags, begTime, dataStep, content });
  return {
    moment: content.map((x) => x.moment),
    timestamp: content.map((x) => x.timestamp),
    // timestring: content.map((x) => x.timestring),
    scaleX: content.map((x) => {
      let diff = dayjs().diff(x.timestamp, "minute");
      let passed = diff < 0 ? 1 : diff > dataStep * 15 ? -1 : 0;
      // let v = dayjs(x.timestamp).format("HHmm");
      let v = dayjs(x.timestamp).format("HH:mm");
      return {
        // value: `${v} +${dataStep * 15}`,
        value: v,
        textStyle: {
          color:
            passed === 1 ? "#409EFF" : passed === 0 ? "#67C23A" : "#909399",
          fontWeight: passed === 0 ? "bold" : "normal",
          opacity: passed === 0 ? 1 : 0.9,
          overflow: "break",
        },
      };
    }),
    data: {
      zuuu: {
        d: content.map((x) => x.ZUUU_D),
        h: content.map((x) => x.ZUUU_H),
      },
      zutf: {
        d: content.map((x) => x.ZUTF_D),
        h: content.map((x) => x.ZUTF_H),
      },
      zumy: {
        d: content.map((x) => x.ZUMY_D),
        h: content.map((x) => x.ZUMY_H),
      },
    },
  };
}
